﻿using ConfExpert.Forms.Base;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevTeam;
using System;
using System.Data;
using System.IO;
using System.Windows.Forms;

namespace ConfExpert.Forms.Teknik
{
    public partial class TeknikCizimEkle : XForm
    {
        public TeknikCizimEkle()
        {
            InitializeComponent();
        }

        bool authorized = false;
        private void TeknikCizimEkle_Load(object sender, EventArgs e)
        {

            authorized = ytk(166);
            gridView1.Init("GetTeknikCizimler");
            gridView1.Columns["YukleyenId"].ColumnEdit = RI(Rp.V_User);
            //gridView1.SetIdentity("PlanlamaId");
            //gridView1.DataTable().SetPK("PlanlamaId");
            gridView1.DataTable().PrimaryKey = new DataColumn[] { gridView1.DataTable().Columns["PlanlamaId"] };

            gridView1.FormatAsDate("DosyaTeslimTarihi", "KayitTarihi");
            gridView1.MakeReadOnly("FasonTarih", "FasonFiyat", "FasonNotlar");
            if (!ytk(58))
            {
                gridView1.MakeColumnInvisible("FasonTarih", "FasonFiyat", "FasonNotlar");
                btnKaydet.Enabled = false;
            }

            gridView1.FormatAsDate("FasonTarih");
            gridView1.AddSummary();
            gridView1.BestFitColumns();

            if (authorized)
            {
                gridView1.Columns["DosyaNo"].ColumnEdit = HyperLink();

                GridManager gm = GridManager.GetManager(gridView1);
                gm.GridMenu.AddMenu("Teknik Çizimi Sil", Sil);
            }

            gridView1.MakeColumnInvisible("TeknikCizimAdi");
            gridView1.Columns["TeknikCizimId"].ColumnEdit = HyperLink();
            gridView1.RowCellClick += gridView1_RowCellClick;

            btnKaydet.ItemClick += btnKaydet_ItemClick;

            bbiGuncelle.ItemClick += bbiGuncelle_ItemClick;
        }

        void bbiGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.RefreshDataSource("GetTeknikCizimler");
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEditing();
            gridView1.RefreshDataSource(false, "UpdTeknikCizimKaydet", gridView1.Serialize());
            gridView1.BestFitColumns();
        }

        void Sil(object sender, EventArgs e)
        {
            gridView1.EndEditing();
            DataRow dr = gridView1.GetDataRow(gridView1.FocusedRowHandle);
            int PlanlamaId = intParse(dr["PlanlamaId"]);
            gridView1.RefreshDataSource(false, "UpdTeknikCizim", PlanlamaId, null, null, null);
            Notify("silindi.");
        }

        public static RepositoryItemHyperLinkEdit HyperLink()
        {
            RepositoryItemHyperLinkEdit riNoteCount = new RepositoryItemHyperLinkEdit();
            riNoteCount.SingleClick = true;
            return riNoteCount;
        }

        void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            int PlanlamaId = 0;
            GridHitInfo hi = gridView1.CalcHitInfo(gridControl1.PointToClient(MousePosition));
            if ((hi.InRow || hi.InRowCell) && gridView1.FocusedRowHandle > -1)
                switch (e.Column.FieldName)
                {
                    case "DosyaNo":
                        if (authorized)
                            if (gridView1.GetFocusedDataRow()["DosyaNo"].ToStr() != string.Empty)
                            {
                                PlanlamaId = gridView1.GetFocusedDataRow()["PlanlamaId"].ToInt(-1);

                                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                                    if (openFileDialog1.OpenFile() != null)
                                    {
                                        FileInfo fi = new FileInfo(openFileDialog1.FileName);
                                        byte[] bArr = FileManager.Read(openFileDialog1.FileName);

                                        gridView1.RefreshDataSource(false, "UpdTeknikCizim", PlanlamaId, fi.Name, bArr, WhoAmI);
                                        gridView1.MakeRowVisible(gridView1.LocateByValue("PlanlamaId", PlanlamaId));

                                        Notify("'" + fi.Name + "' eklendi.");
                                        gridView1.BestFitColumns();
                                    }
                            }

                        break;


                    case "TeknikCizimId":
                        PlanlamaId = intParse(gridView1.GetFocusedRowCellValue("PlanlamaId"));
                        if (gridView1.GetFocusedRowCellDisplayText("TeknikCizimAdi") != string.Empty)
                            if (PlanlamaId > 0)
                            {
                                DataTable t = ExecuteDataTable("GetTeknikCizim", PlanlamaId);
                                FileManager.RunTempFile(t.Rows[0]["TeknikCizimAdi"].ToString(), t.Rows[0]["BLOB"] as byte[]);
                            }
                        break;
                }
        }

    }
}
